package com.tjetc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.common.Result;
import com.tjetc.common.Result;
import com.tjetc.domain.Department;
import com.tjetc.domain.Doctor;
import com.tjetc.domain.News;
import com.tjetc.service.NewsService;
import com.tjetc.service.impl.NewsServiceImpl;
import com.tjetc.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@WebServlet("/news/*")
@MultipartConfig
public class NewsServlet extends HttpServlet {
    private NewsService newsService=new NewsServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void listNewsBySid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int sid = Integer.valueOf(req.getParameter("sid"));
        List<News> news=newsService.findBySid(sid);
        new ObjectMapper().writeValue(resp.getOutputStream(),news);
    }

    //用zSet结构来回显热点新闻数据
    protected void listHotNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Set<String> news=newsService.listHotNews();
        System.out.println("热点新闻数据news = " + news);
        new ObjectMapper().writeValue(resp.getOutputStream(),news);
    }
    //给redis中数据加分数，重新排序
    protected void addScore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        boolean b = newsService.addScore(id);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("加分成功"));
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("加分失败"));
    }


    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String content =req.getParameter("content");
        String curPage=req.getParameter("curPage");
        String ps =req.getParameter("pageSize");
        content=content==null?"":content;
        int currentPage = curPage ==null ? 1:Integer.valueOf(curPage);
        int pageSize = ps ==null ? 5 :Integer.parseInt(ps);
        int count = newsService.getCount(content);
        PageInfo<News> pageInfo=new PageInfo<>(currentPage,count,pageSize);
        List<News> list = newsService.list(content,pageInfo);
        pageInfo.setList(list);
        new ObjectMapper().writeValue(resp.getOutputStream(),pageInfo);

    }


    protected void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        boolean b=newsService.del(id);
        if (b){
            new ObjectMapper().writeValue(resp.getOutputStream(), Result.success("删除成功"));
        }
        new ObjectMapper().writeValue(resp.getOutputStream(), Result.fail("删除失败"));

    }


    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String content = request.getParameter("content");
        Integer sid = Integer.valueOf(request.getParameter("sid"));
        News news = new News();
        news.setContent(content);
        news.setSid(sid);
        boolean b = newsService.add(news);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("添加成功"));
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("添加失败，出异常喽！"));
    }


    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id= Integer.parseInt(request.getParameter("id"));
        String content=request.getParameter("content");
        int sid= Integer.valueOf(request.getParameter("sid"));
        News news=new News();
        news.setId(id);
        news.setContent(content);
        news.setSid(sid);
        boolean b = newsService.update(news);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("修改成功"));
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("修改失败"));
    }

}
